﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PhoneBilling.Models;
using PhoneBilling.Report;
using System.IO;

namespace PhoneBilling.Controllers
{
    public class StatisticsController : Controller
    {
        //
        // GET: /Statistics/

        public ActionResult Index()
        {
            if (Session["User"] == null)
            {
                return this.RedirectToAction("../Account/Login");
            }

            MenuModel model = (MenuModel)Session["Menu"];
            model.SetMenu("Statistics");

            return View();
        }

        public ActionResult Report1()
        {
            if (Session["User"] == null)
            {
                return this.RedirectToAction("../Account/Login");
            }

            SanDiDbsEntities entities = new SanDiDbsEntities();

            Guid userID = (Guid)Session["UserID"];
            var authroity = (from a in entities.UserAuthority
                             where a.Item == "待结账统计" &&
                                   a.UserID == userID
                             select a).SingleOrDefault();
            if (authroity == null || !authroity.Allow)
            {
                TempData["Error"] = "你没有权限使用这个功能";
                return RedirectToAction("Error", "Shared");
            }

            MenuModel model1 = (MenuModel)Session["Menu"];
            model1.SetMenu("Statistics", "Statistics/Report1");

            Report1Model model = new Report1Model();
            model.DepartmentList = getDepartmentList(entities);
            model.收费等级List = loadGrade();

            return View(model);
        }

        private List<UIDepartment> getDepartmentList(SanDiDbsEntities entities)
        {
            return getDepartmentList(entities, true);
        }

        private List<UIDepartment> getDepartmentList(SanDiDbsEntities entities , bool hasExtension)
        {
            List<UIDepartment> departmentList = (from d in entities.NDepartment
                                                 orderby d.Seq
                                                 select new UIDepartment()
                                                 {
                                                     名称 = d.Name.Trim(),
                                                     ID = d.ID,
                                                 }).ToList();

            foreach (var department in departmentList)
            {
                department.分组List = new List<UIGroup>();

                var list1 = from g in entities.NGroup
                            where g.ParentType.Trim() == "Department" &&
                                  g.ParentID == department.ID
                            orderby g.Seq
                            select g;
                foreach (var group1 in list1)
                {
                    UIGroup uiGroup1 = new UIGroup();
                    uiGroup1.ID = group1.ID.Trim();
                    uiGroup1.名称 = group1.Name.Trim();
                    uiGroup1.分组List = new List<UIGroup>();

                    department.分组List.Add(uiGroup1);

                    if (hasExtension)
                    {
                        var eList3 = from g in entities.Extensions
                                     where g.GrpNo.Trim() == uiGroup1.ID
                                     orderby g.Seq,g.ExtNo
                                     select g.ExtNo;
                        uiGroup1.ExtensionList = new List<UIExtension>();
                        foreach (var item in eList3)
                        {
                            UIExtension extension = new UIExtension();
                            extension.分机号码 = item.Trim();
                            uiGroup1.ExtensionList.Add(extension);
                        }
                    }

                    var list2 = from g in entities.NGroup
                                where g.ParentType.Trim() == "Group" &&
                                      g.ParentID == uiGroup1.ID
                                orderby g.Seq
                                select g;
                    foreach (var group2 in list2)
                    {
                        UIGroup uiGroup2 = new UIGroup();
                        uiGroup2.ID = group2.ID.Trim();
                        uiGroup2.名称 = group2.Name.Trim();
                        uiGroup2.分组List = new List<UIGroup>();

                        uiGroup1.分组List.Add(uiGroup2);

                        if (hasExtension)
                        {
                            var eList2 = from g in entities.Extensions
                                         where g.GrpNo.Trim() == uiGroup2.ID
                                         orderby g.Seq, g.ExtNo
                                         select g.ExtNo;
                            uiGroup2.ExtensionList = new List<UIExtension>();
                            foreach (var item in eList2)
                            {
                                UIExtension extension = new UIExtension();
                                extension.分机号码 = item.Trim();
                                uiGroup2.ExtensionList.Add(extension);
                            }
                        }
                        var list3 = from g in entities.NGroup
                                    where g.ParentType.Trim() == "Group" &&
                                          g.ParentID == uiGroup2.ID
                                    orderby g.Seq
                                    select g;
                        foreach (var group3 in list3)
                        {
                            UIGroup uiGroup3 = new UIGroup();
                            uiGroup3.ID = group3.ID.Trim();
                            uiGroup3.名称 = group3.Name.Trim();
                            uiGroup3.分组List = new List<UIGroup>();

                            uiGroup2.分组List.Add(uiGroup3);

                            if (hasExtension)
                            {
                                var eList1 = from g in entities.Extensions
                                             where g.GrpNo.Trim() == uiGroup3.ID
                                             orderby g.Seq, g.ExtNo
                                             select g.ExtNo;
                                uiGroup3.ExtensionList = new List<UIExtension>();
                                foreach (var item in eList1)
                                {
                                    UIExtension extension = new UIExtension();
                                    extension.分机号码 = item.Trim();
                                    uiGroup3.ExtensionList.Add(extension);
                                }
                            }
                        }


                    }
                }
            }

            return departmentList;
        }

        private List<UIGrade> loadGrade()
        {
            SanDiDbsEntities entities = new SanDiDbsEntities();
            var list = from obj in entities.OBJDefine
                       where obj.ObjectID.Trim() == "GRADE"
                       orderby obj.ItemID
                       select obj;
            var traiffList = new List<UIGrade>() { new UIGrade(){ID= string.Empty,Name = string.Empty}};
            foreach (var item in list)
            {
                if (item.ItemID.Trim() == "R")
                    continue;

                UIGrade traiff = new UIGrade();
                traiff.ID = item.ItemID.Trim();
                traiff.Name = string.Format("{0}.{1}", item.ItemID.Trim(), item.ItemName.Trim());
                traiff.Remark = item.Comment == null ? null : item.Comment.Trim();
                traiffList.Add(traiff);
            }

            return traiffList;
        }

        [HttpPost]
        public ActionResult Report1(Report1Model model)
        {
            if (model.SelectItem == null || model.SelectItem.Trim() == string.Empty)
            {
                TempData["Error"] = "请选择部门、分组";
                return RedirectToAction("Error", "Shared");
            }
            try
            {
                if (model.SelectItem == null)
                    model.SelectItem = "";

                string[] idList = model.SelectItem.Split('!');
                List<string> list = idList.ToList();

                SanDiDbsEntities entities = new SanDiDbsEntities();

                if(model.收费等级 != null && model.收费等级 != string.Empty)
                foreach (var id in idList)
                {
                    var extension = (from e in entities.Extensions
                                     where e.ExtNo.Trim() == id
                                     select e).SingleOrDefault();
                    if (extension.GradeID != model.收费等级)
                        list.Remove(id);
                }

                var list1 = from a in entities.NAccount
                           where a.HasPay == false &&
                                 list.Contains(a.ExtensionID.Trim())
                           orderby a.DepartmentID, a.GroupID, a.ExtensionID, a.Month
                           select a;

                List<UIAccount> resultList = new List<UIAccount>();

                foreach (var item in list1)
                {
                    UIAccount account = new UIAccount();
                    account.ID = item.ID;
                    account.月 = string.Format("{0}年{1}月", item.Month.Year, item.Month.Month);
                    var dept = (from d in entities.NDepartment
                                where d.ID.Trim() == item.DepartmentID.Trim()
                                select d).SingleOrDefault();
                    account.部门 = (dept == null) ? "" : dept.Name.Trim();
                    var group = (from d in entities.NGroup
                                 where d.ID.Trim() == item.GroupID.Trim()
                                 select d).SingleOrDefault();
                    account.分组 = (group == null) ? "" : group.Name.Trim();
                    account.分机号码 = item.ExtensionID.Trim();
                    var ext = (from e in entities.Extensions
                               where e.ExtNo.Trim() == account.分机号码
                               select e).SingleOrDefault();
                    if (ext != null && ext.UserName != null)
                        account.用户名 = ext.UserName.Trim();

                    account.月租费 = item.Fee1.ToString("0.00");
                    account.功能费 = item.Fee2.ToString("0.00");
                    account.通话费 = (item.TotalFee - item.Fee1 - item.Fee2).ToString("0.00");
                    account.话单总次数 = item.TotalPhn;
                    account.话单总金额 = item.TotalFee.ToString("0.00");
                    account.IsSelected = true;

                    resultList.Add(account);
                }

                TempData["Result"] = resultList;

                if (model.IsExport)
                    return RedirectToAction("Report1Report", "Statistics");
                else
                {
                    TempData["Model"] = model;
                    return RedirectToAction("Report1Result", "Statistics");
                }
            }
            catch (Exception e)
            {
                TempData["Error"] = e.Message;
                return RedirectToAction("Error", "Shared");
            }
        }

        public ActionResult Report1Result()
        {
            Report1Model report1Model = (Report1Model)TempData["Model"];

            Report1ResultModel model = new Report1ResultModel();
            model.List = (List<UIAccount>)TempData["Result"];
            model.总金额 = (model.List.Sum(i => Convert.ToDecimal(i.话单总金额))).ToString("0.00");
            TempData["Result"] = null;
            model.SelectItem = report1Model.SelectItem;
            model.收费等级 = report1Model.收费等级;
            return View(model);
        }

        public FileContentResult Report1Report()
        {
            List<UIAccount> accountList = (List<UIAccount>)TempData["Result"];
            //List<UISourcePhn> resultList = new List<UISourcePhn>();
            ReportResultModel model = new ReportResultModel();
            model.总金额 = (accountList.Sum(i => Convert.ToDecimal(i.话单总金额))).ToString("0.00");

            RptReport1 report = new RptReport1();
            report.Source.DataSource = accountList;
            report.TotalSource.DataSource = model;
            using (MemoryStream stream = new MemoryStream())
            {
                report.ExportToXls(stream);
                byte[] b = new byte[stream.Length];
                stream.Position = 0;
                stream.Read(b, 0, b.Length);
                return File(b, "application/octet-stream", "未付账报表.xls");
            }

        }

        public ActionResult Report2()
        {
            if (Session["User"] == null)
            {
                return this.RedirectToAction("../Account/Login");
            }

            SanDiDbsEntities entities = new SanDiDbsEntities();

            Guid userID = (Guid)Session["UserID"];
            var authroity = (from a in entities.UserAuthority
                             where a.Item == "账目统计" &&
                                   a.UserID == userID
                             select a).SingleOrDefault();
            if (authroity == null || !authroity.Allow)
            {
                TempData["Error"] = "你没有权限使用这个功能";
                return RedirectToAction("Error", "Shared");
            }

            MenuModel model1 = (MenuModel)Session["Menu"];
            model1.SetMenu("Statistics", "Statistics/Report2");

            Report2Model model = new Report2Model();
            model.DepartmentList = getDepartmentList(entities);

            model.开始年 = model.结束年 = DateTime.Now.Year;
            model.开始月 = model.结束月 = DateTime.Now.Month;
            model.年List = new List<int>();
            model.年List.Add(model.开始年 - 5);
            model.年List.Add(model.开始年 - 4);
            model.年List.Add(model.开始年 - 3);
            model.年List.Add(model.开始年 - 2);
            model.年List.Add(model.开始年 - 1);
            model.年List.Add(model.开始年);
            model.年List.Add(model.开始年 + 1);

            model.月List = new List<int>();
            model.月List.Add(1);
            model.月List.Add(2);
            model.月List.Add(3);
            model.月List.Add(4);
            model.月List.Add(5);
            model.月List.Add(6);
            model.月List.Add(7);
            model.月List.Add(8);
            model.月List.Add(9);
            model.月List.Add(10);
            model.月List.Add(11);
            model.月List.Add(12);

            model.支付状态 = "全部";

            return View(model);
        }

        [HttpPost]
        public ActionResult Report2(Report2Model model)
        {
            if (model.SelectItem == null || model.SelectItem.Trim() == string.Empty)
            {
                TempData["Error"] = "请选择部门、分组";
                return RedirectToAction("Error", "Shared");
            }

            try
            {
                if (model.SelectItem == null)
                    model.SelectItem = "";

                string[] idList = model.SelectItem.Split('!');

                DateTime startDate = new DateTime(model.开始年, model.开始月, 1); ;
                DateTime endDate = new DateTime(model.结束年, model.结束月, 1);

                SanDiDbsEntities entities = new SanDiDbsEntities();

                var temp = from a in entities.NAccount
                           where a.Month >= startDate &&
                                 a.Month <= endDate &&
                                 idList.Contains(a.ExtensionID.Trim())
                           select a;
                IOrderedQueryable<NAccount> list = null;

                switch (model.支付状态)
                { 
                    case "全部":
                        list = from a in temp
                               orderby a.DepartmentID, a.GroupID, a.ExtensionID, a.Month
                               select a;
                        break;
                    case "已支付":
                        list = from a in temp
                               where a.HasPay
                               orderby a.DepartmentID, a.GroupID, a.ExtensionID, a.Month
                               select a;
                        break;
                    case "未支付":
                        list = from a in temp
                               where !a.HasPay
                               orderby a.DepartmentID, a.GroupID, a.ExtensionID, a.Month
                               select a;
                        break;
                }

                List<UIAccount> resultList = new List<UIAccount>();

                foreach (var item in list)
                {
                    UIAccount account = new UIAccount();
                    account.ID = item.ID;
                    account.月 = string.Format("{0}年{1}月", item.Month.Year, item.Month.Month);
                    var dept = (from d in entities.NDepartment
                                where d.ID.Trim() == item.DepartmentID.Trim()
                                select d).SingleOrDefault();
                    account.部门 = (dept == null) ? "" : dept.Name.Trim();
                    var group = (from d in entities.NGroup
                                 where d.ID.Trim() == item.GroupID.Trim()
                                 select d).SingleOrDefault();
                    account.分组 = (group == null) ? "" : group.Name.Trim();
                    account.分机号码 = item.ExtensionID.Trim();
                    var ext = (from e in entities.Extensions
                               where e.ExtNo.Trim() == account.分机号码
                               select e).SingleOrDefault();
                    if (ext != null && ext.UserName != null)
                        account.用户名 = ext.UserName.Trim();

                    account.月租费 = item.Fee1.ToString("0.00");
                    account.功能费 = item.Fee2.ToString("0.00");
                    account.通话费 = (item.TotalFee - item.Fee1 - item.Fee2).ToString("0.00");
                    account.话单总次数 = item.TotalPhn;
                    account.话单总金额 = item.TotalFee.ToString("0.00");
                                        
                    account.已经收款 = item.HasPay ? "是" : "否";
                    account.IsSelected = true;

                    resultList.Add(account);
                }

                TempData["Result"] = resultList;

                if (model.IsExport)
                    return RedirectToAction("Report2Report", "Statistics");
                {
                    TempData["Model"] = model;
                    return RedirectToAction("Report2Result", "Statistics");
                }
            }
            catch (Exception e)
            {
                TempData["Error"] = e.Message;
                return RedirectToAction("Error", "Shared");
            }
        }

        public ActionResult Report2Result()
        {
            Report2ResultModel model = new Report2ResultModel();
            model.List = (List<UIAccount>)TempData["Result"];
            model.总金额 = (model.List.Sum(i => Convert.ToDecimal(i.话单总金额))).ToString("0.00");
            TempData["Result"] = null;
            Report2Model report1Model = (Report2Model)TempData["Model"];
            model.SelectItem = report1Model.SelectItem;
            model.开始年 = report1Model.开始年;
            model.开始月 = report1Model.开始月;
            model.结束年 = report1Model.结束年;
            model.结束月 = report1Model.结束月;
            model.支付状态 = report1Model.支付状态;
            return View(model);
        }

        public FileContentResult Report2Report()
        {
            List<UIAccount> accountList = (List<UIAccount>)TempData["Result"];
            //List<UISourcePhn> resultList = new List<UISourcePhn>();
            ReportResultModel model = new ReportResultModel();
            model.总金额 = (accountList.Sum(i => Convert.ToDecimal(i.话单总金额))).ToString("0.00");

            RptReport2 report = new RptReport2();
            report.Source.DataSource = accountList;
            report.TotalSource.DataSource = model;
            using (MemoryStream stream = new MemoryStream())
            {
                report.ExportToXls(stream);
                byte[] b = new byte[stream.Length];
                stream.Position = 0;
                stream.Read(b, 0, b.Length);
                return File(b, "application/octet-stream", "账目报表.xls");
            }

        }

        public ActionResult Report3()
        {
            if (Session["User"] == null)
            {
                return this.RedirectToAction("../Account/Login");
            }

            SanDiDbsEntities entities = new SanDiDbsEntities();

            Guid userID = (Guid)Session["UserID"];
            var authroity = (from a in entities.UserAuthority
                             where a.Item == "话务统计" &&
                                   a.UserID == userID
                             select a).SingleOrDefault();
            if (authroity == null || !authroity.Allow)
            {
                TempData["Error"] = "你没有权限使用这个功能";
                return RedirectToAction("Error", "Shared");
            }

            MenuModel model1 = (MenuModel)Session["Menu"];
            model1.SetMenu("Statistics", "Statistics/Report3");

            Report3Model model = new Report3Model();
            model.起始时间 = model.起始时间Time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            model.终止时间 = model.终止时间Time = model.起始时间.AddMonths(1).AddSeconds(-1);
            model.所有话类 = true;
            model.话类List = loadAreaTypeList();
            
            model.DepartmentList = getDepartmentList(entities);

            return View(model);
        }

        private string findDepartmentID(Extensions extension, SanDiDbsEntities entities)
        {
            NGroup ngroup = (from g in entities.NGroup
                             where g.ID.Trim() == extension.GrpNo.Trim()
                             select g).SingleOrDefault();
            while (ngroup.ParentType == "Group")
            {
                ngroup = (from g in entities.NGroup
                          where g.ID.Trim() == ngroup.ParentID.Trim()
                          select g).SingleOrDefault();
            }
            return ngroup.ParentID.Trim();
        }
        
        [HttpPost]
        public ActionResult Report3(Report3Model model)
        {
            if (model.SelectItem == null || model.SelectItem.Trim() == string.Empty)
            {
                TempData["Error"] = "请选择分机";
                return RedirectToAction("Error", "Shared");
            }

            try
            {
                if (model.SelectItem == null)
                    model.SelectItem = "";

                string[] idList = model.SelectItem.Split('!');

                List<string> 话类chars = new List<string>();
                if (!string.IsNullOrEmpty(model.话类))
                {
                    char[] temp = model.话类.ToArray();

                    foreach (var c in temp)
                        话类chars.Add(c.ToString());
                }

                SanDiDbsEntities entities = new SanDiDbsEntities();

                DateTime startDate = new DateTime(model.起始时间.Year, model.起始时间.Month, model.起始时间.Day, model.起始时间.Hour, model.起始时间.Minute, model.起始时间.Second);
                DateTime endDate = new DateTime(model.终止时间.Year, model.终止时间.Month, model.终止时间.Day, model.终止时间.Hour, model.终止时间.Minute, model.终止时间.Second);

                var extensionList = from e in entities.Extensions
                                    where idList.Contains(e.ExtNo.Trim())
                                    select e;
                List<UIAccount> resultList = new List<UIAccount>();

                foreach (var extension in extensionList)
                {
                    UIAccount account = new UIAccount();
                    
                    account.分机号码 = extension.ExtNo.Trim();
                    var group = (from g in entities.NGroup
                                 where g.ID.Trim() == extension.GrpNo.Trim()
                                 select g).SingleOrDefault();
                    if (group == null)
                        account.分组 = string.Empty;
                    else
                        account.分组 = group.Name.Trim();

                    var temp = group;
                    while (temp.ParentType.Trim() == "Group")
                    {
                        temp = (from g in entities.NGroup
                                where g.ID.Trim() == temp.ParentID
                                select g).SingleOrDefault();
                    }
                    var parent = (from g in entities.NDepartment
                                  where g.ID.Trim() == temp.ParentID
                                  select g).SingleOrDefault();

                    account.部门 = parent.Name.Trim();

                    if (model.话类 == null)
                        model.话类 = string.Empty;
                    char[] chars = model.话类.ToArray();
                    List<string> strs = new List<string>();
                    foreach (var c in chars)
                        strs.Add(c.ToString());


                    var phnList = (from p in entities.PhnToday
                                   where p.CallerID.Trim() == account.分机号码 &&
                                         p.EndTime >= startDate &&
                                         p.EndTime <= endDate &&
                                         p.Slices != null  && p.BaseFee != null && p.ServeFee != null && p.ExtraFee != null &&
                                         (model.所有话类 || strs.Contains(p.AreaType.Trim()))
                                   select p).ToList();
                    
                    var tempList = (from p in phnList
                                    where p.AreaType.Trim() == "I"
                                    select p);
                    if (tempList.Count() == 0)
                    {
                        //account.内线计次 = "0";
                        account.内线金额 = "0.00";
                    }
                    else
                    {
                        //account.内线计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.内线金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "C"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.市话计次 = "0";
                        account.市话金额 = "0.00";
                    }
                    else
                    {
                        //account.市话计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.市话金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "F"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.国际计次 = "0";
                        account.国际金额 = "0.00";
                    }
                    else
                    {
                        //account.国际计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.国际金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "L"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.国内计次 = "0";
                        account.国内金额 = "0.00";
                    }
                    else
                    {
                        //account.国内计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.国内金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "M"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.信息计次 = "0";
                        account.信息金额 = "0.00";
                    }
                    else
                    {
                        //account.信息计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.信息金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "O"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.其他计次 = "0";
                        account.其他金额 = "0.00";
                    }
                    else
                    {
                        //account.其他计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.其他金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "P"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.IP计次 = "0";
                        account.IP金额 = "0.00";
                    }
                    else
                    {
                        //account.IP计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.IP金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "S"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.郊县计次 = "0";
                        account.郊县金额 = "0.00";
                    }
                    else
                    {
                        //account.郊县计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.郊县金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "Z"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.专线计次 = "0";
                        account.专线金额 = "0.00";
                    }
                    else
                    {
                        //account.专线计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.专线金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    //account.话单总次数 = phnList.Count();
                    account.总金额 = phnList.Sum((item) => item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value);
                    account.话单总金额 = account.总金额.ToString("0.00");

                    resultList.Add(account);
                }

                TempData["Result"] = resultList;

                if (model.IsExport)
                    return RedirectToAction("Report3Report", "Statistics");
                else
                {
                    TempData["Model"] = model;
                    return RedirectToAction("Report3Result", "Statistics");
                }
            }
            catch (Exception e)
            {
                TempData["Error"] = e.Message;
                return RedirectToAction("Error", "Shared");
            }
        }

        public ActionResult Report3Result()
        {
            Report3ResultModel model = new Report3ResultModel();
            model.List = (List<UIAccount>)TempData["Result"];
            model.总金额 = (model.List.Sum(i => Convert.ToDecimal(i.话单总金额))).ToString("0.00");
            TempData["Result"] = null;
            Report3Model report1Model = (Report3Model)TempData["Model"];
            model.SelectItem = report1Model.SelectItem;
            model.起始时间 = report1Model.起始时间;
            model.起始时间Time = report1Model.起始时间Time;
            model.终止时间 = report1Model.终止时间;
            model.终止时间Time = report1Model.终止时间Time;
            RptImgReport3 report = new RptImgReport3();
            report.Source.DataSource = model.List;
            report.TotalSource.DataSource = model;
            using (MemoryStream stream = new MemoryStream())
            {
                report.ExportToHtml(stream);
                StreamReader sr = new StreamReader(stream);
                stream.Position = 0;
                model.Content = sr.ReadToEnd();
                
            }
            return View(model);
        }

        public FileContentResult Report3Report()
        {
            List<UIAccount> accountList = (List<UIAccount>)TempData["Result"];
            //List<UISourcePhn> resultList = new List<UISourcePhn>();
            ReportResultModel model = new ReportResultModel();
            model.总金额 = (accountList.Sum(i => Convert.ToDecimal(i.话单总金额))).ToString("0.00");

            RptReport3 report = new RptReport3();
            report.Source.DataSource = accountList;
            report.TotalSource.DataSource = model;
            using (MemoryStream stream = new MemoryStream())
            {
                report.ExportToXls(stream);
                byte[] b = new byte[stream.Length];
                stream.Position = 0;
                stream.Read(b, 0, b.Length);
                return File(b, "application/octet-stream", "分机话务报表.xls");
            }

        }

        public ActionResult Report4()
        {
            if (Session["User"] == null)
            {
                return this.RedirectToAction("../Account/Login");
            }

            SanDiDbsEntities entities = new SanDiDbsEntities();

            Guid userID = (Guid)Session["UserID"];
            var authroity = (from a in entities.UserAuthority
                             where a.Item == "话务统计" &&
                                   a.UserID == userID
                             select a).SingleOrDefault();
            if (authroity == null || !authroity.Allow)
            {
                TempData["Error"] = "你没有权限使用这个功能";
                return RedirectToAction("Error", "Shared");
            }

            MenuModel model1 = (MenuModel)Session["Menu"];
            model1.SetMenu("Statistics", "Statistics/Report4");

            Report4Model model = new Report4Model();
            model.起始时间 = model.起始时间Time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            model.终止时间 = model.终止时间Time = model.起始时间.AddMonths(1).AddSeconds(-1);
            model.所有话类 = true;
            model.话类List = loadAreaTypeList();
            model.DepartmentList = (from d in entities.NDepartment
                                    orderby d.Seq
                                    select new UIDepartment()
                                    {
                                        名称 = d.Name.Trim(),
                                        ID = d.ID,
                                    }).ToList();
            return View(model);
        }

        [HttpPost]
        public ActionResult Report4(Report4Model model)
        {
            try
            {
                if (model.SelectItem == null)
                    model.SelectItem = "";

                string[] idList = model.SelectItem.Split('!');
               
                SanDiDbsEntities entities = new SanDiDbsEntities();

                DateTime startDate = new DateTime(model.起始时间.Year, model.起始时间.Month, model.起始时间.Day, model.起始时间.Hour, model.起始时间.Minute, model.起始时间.Second);
                DateTime endDate = new DateTime(model.终止时间.Year, model.终止时间.Month, model.终止时间.Day, model.终止时间.Hour, model.终止时间.Minute, model.终止时间.Second);

                if (model.话类 == null)
                    model.话类 = string.Empty;
                char[] chars = model.话类.ToArray();
                List<string> strs = new List<string>();
                foreach (var c in chars)
                    strs.Add(c.ToString());

                var departmentList = from e in entities.NDepartment
                                    where idList.Contains(e.ID)
                                    orderby e.Seq
                                    select e;
                List<UIAccount> resultList = new List<UIAccount>();

                foreach (var department in departmentList)
                {
                    List<string> extensionList = getExtensionList(department, entities);

                    UIAccount account = new UIAccount();
                    account.部门 = department.Name.Trim();
                    account.用户数 = extensionList.Count();
                    if (account.用户数 == 0)
                    {
                        account.IP金额 = "0.00";
                        account.国际金额 = "0.00";
                        account.国内金额 = "0.00";
                        account.话单总金额 = "0.00";
                        account.郊县金额 = "0.00";
                        account.内线金额 = "0.00";
                        account.其他金额 = "0.00";
                        account.市话金额 = "0.00";
                        account.信息金额 = "0.00";
                        account.专线金额 = "0.00";

                        resultList.Add(account);
                        continue;
                    }

                    var phnList = (from p in entities.PhnToday
                                   where extensionList.Contains(p.CallerID.Trim()) &&
                                         p.EndTime >= startDate &&
                                         p.EndTime <= endDate &&
                                         p.Slices != null  && p.BaseFee != null && p.ServeFee != null && p.ExtraFee != null &&
                                         (model.所有话类 || strs.Contains(p.AreaType.Trim()))
                                   select p).ToList();

                    var tempList = (from p in phnList
                                    where p.AreaType.Trim() == "I"
                                    select p);
                    if (tempList.Count() == 0)
                    {
                        //account.内线计次 = "0";
                        account.内线金额 = "0.00";
                    }
                    else
                    {
                        //account.内线计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.内线金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00") ;
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "C"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.市话计次 = "0";
                        account.市话金额 = "0.00";
                    }
                    else
                    {
                        //account.市话计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.市话金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00") ;
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "F"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.国际计次 = "0";
                        account.国际金额 = "0.00";
                    }
                    else
                    {
                        //account.国际计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.国际金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "L"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.国内计次 = "0";
                        account.国内金额 = "0.00";
                    }
                    else
                    {
                        //account.国内计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.国内金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00") ;
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "M"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.信息计次 = "0";
                        account.信息金额 = "0.00";
                    }
                    else
                    {
                        //account.信息计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.信息金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "O"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.其他计次 = "0";
                        account.其他金额 = "0.00";
                    }
                    else
                    {
                        //account.其他计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.其他金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00") ;
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "P"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.IP计次 = "0";
                        account.IP金额 = "0.00";
                    }
                    else
                    {
                        //account.IP计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.IP金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00") ;
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "S"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.郊县计次 = "0";
                        account.郊县金额 = "0.00";
                    }
                    else
                    {
                        //account.郊县计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.郊县金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "Z"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.专线计次 = "0";
                        account.专线金额 = "0.00";
                    }
                    else
                    {
                        //account.专线计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.专线金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    //account.话单总次数 = phnList.Count();
                    account.总金额 = phnList.Sum((item) => item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value);
                    account.话单总金额 = account.总金额.ToString("0.00");

                    resultList.Add(account);
                }

                TempData["Result"] = resultList;

                if (model.IsExport)
                    return RedirectToAction("Report4Report", "Statistics");
                else
                {
                    TempData["Model"] = model;
                    return RedirectToAction("Report4Result", "Statistics");
                }
            }
            catch (Exception e)
            {
                TempData["Error"] = e.Message;
                return RedirectToAction("Error", "Shared");
            }
        }

        private List<string> getExtensionList(NDepartment department, SanDiDbsEntities entities)
        {
            List<string> list = new List<string>();
            var gourpList = (from g in entities.NGroup
                             where g.ParentType.Trim() == "Department" &&
                                   g.ParentID.Trim() == department.ID.Trim()
                             select g);

            foreach (var ngroup in gourpList)
                list.AddRange(getExtensionList(ngroup, entities));

            return list;

        }

        private List<string> getExtensionList(NGroup ngroup, SanDiDbsEntities entities)
        {
            List<string> list = (from e in entities.Extensions
                                 where e.GrpNo.Trim() == ngroup.ID.Trim()
                                 select e.ExtNo.Trim()).ToList();

            var gourpList = (from g in entities.NGroup
                             where g.ParentType.Trim() == "Group" &&
                                   g.ParentID.Trim() == ngroup.ID.Trim()
                             select g);

            foreach (var temp in gourpList)
                list.AddRange(getExtensionList(temp, entities));

            return list;
        }

        public ActionResult Report4Result()
        {
            Report4ResultModel model = new Report4ResultModel();
            model.List = (List<UIAccount>)TempData["Result"];
            model.总金额 = (model.List.Sum(i => Convert.ToDecimal(i.话单总金额))).ToString("0.00");
            TempData["Result"] = null;
            Report4Model report1Model = (Report4Model)TempData["Model"];
            model.SelectItem = report1Model.SelectItem;
            model.起始时间 = report1Model.起始时间;
            model.起始时间Time = report1Model.起始时间Time;
            model.终止时间 = report1Model.终止时间;
            model.终止时间Time = report1Model.终止时间Time;

            RptImgReport4 report = new RptImgReport4();
            report.Source.DataSource = model.List;
            report.TotalSource.DataSource = model;
            report.ItemCount = model.List.Count;
            using (MemoryStream stream = new MemoryStream())
            {
                report.ExportToHtml(stream);
                StreamReader sr = new StreamReader(stream);
                stream.Position = 0;
                model.Content = sr.ReadToEnd();

            }
            return View(model);
        }

        public FileContentResult Report4Report()
        {
            List<UIAccount> accountList = (List<UIAccount>)TempData["Result"];
            //List<UISourcePhn> resultList = new List<UISourcePhn>();
            ReportResultModel model = new ReportResultModel();
            model.总金额 = (accountList.Sum(i => Convert.ToDecimal(i.话单总金额))).ToString("0.00");

            RptReport4 report = new RptReport4();
            report.Source.DataSource = accountList;
            report.TotalSource.DataSource = model;
            report.ItemCount = accountList.Count;
            using (MemoryStream stream = new MemoryStream())
            {
                report.ExportToXls(stream);
                byte[] b = new byte[stream.Length];
                stream.Position = 0;
                stream.Read(b, 0, b.Length);
                return File(b, "application/octet-stream", "部门话务报表.xls");
            }

        }

        public ActionResult Report9()
        {
            if (Session["User"] == null)
            {
                return this.RedirectToAction("../Account/Login");
            }

            SanDiDbsEntities entities = new SanDiDbsEntities();

            Guid userID = (Guid)Session["UserID"];
            var authroity = (from a in entities.UserAuthority
                             where a.Item == "话务统计" &&
                                   a.UserID == userID
                             select a).SingleOrDefault();
            if (authroity == null || !authroity.Allow)
            {
                TempData["Error"] = "你没有权限使用这个功能";
                return RedirectToAction("Error", "Shared");
            }

            MenuModel model1 = (MenuModel)Session["Menu"];
            model1.SetMenu("Statistics", "Statistics/Report9");

            Report4Model model = new Report4Model();
            model.起始时间 = model.起始时间Time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            model.终止时间 = model.终止时间Time = model.起始时间.AddMonths(1).AddSeconds(-1);
            model.所有话类 = true;
            model.话类List = loadAreaTypeList();
            
            model.DepartmentList = getDepartmentList(entities, false);

            return View(model);
        }

        [HttpPost]
        public ActionResult Report9(Report4Model model)
        {
            try
            {
                if (model.SelectItem == null)
                    model.SelectItem = "";

                string[] idList = model.SelectItem.Split('!');

                SanDiDbsEntities entities = new SanDiDbsEntities();

                DateTime startDate = new DateTime(model.起始时间.Year, model.起始时间.Month, model.起始时间.Day, model.起始时间.Hour, model.起始时间.Minute, model.起始时间.Second);
                DateTime endDate = new DateTime(model.终止时间.Year, model.终止时间.Month, model.终止时间.Day, model.终止时间.Hour, model.终止时间.Minute, model.终止时间.Second);

                if (model.话类 == null)
                    model.话类 = string.Empty;
                char[] chars = model.话类.ToArray();
                List<string> strs = new List<string>();
                foreach (var c in chars)
                    strs.Add(c.ToString());

                var groupList = from e in entities.NGroup
                                     where idList.Contains(e.ID)
                                     orderby e.Seq
                                     select e;
                List<UIAccount> resultList = new List<UIAccount>();

                foreach (var group in groupList)
                {
                    List<string> extensionList = getExtensionList(group, entities);

                    UIAccount account = new UIAccount();
                    account.分组 = group.Name.Trim();
                    account.用户数 = extensionList.Count();
                    if (account.用户数 == 0)
                    {
                        account.IP金额 = "0.00";
                        account.国际金额 = "0.00";
                        account.国内金额 = "0.00";
                        account.话单总金额 = "0.00";
                        account.郊县金额 = "0.00";
                        account.内线金额 = "0.00";
                        account.其他金额 = "0.00";
                        account.市话金额 = "0.00";
                        account.信息金额 = "0.00";
                        account.专线金额 = "0.00";

                        resultList.Add(account);
                        continue;
                    }

                    var phnList = (from p in entities.PhnToday
                                   where extensionList.Contains(p.CallerID.Trim()) &&
                                         p.EndTime >= startDate &&
                                         p.EndTime <= endDate &&
                                         p.Slices != null && p.BaseFee != null && p.ServeFee != null && p.ExtraFee != null &&
                                         (model.所有话类 || strs.Contains(p.AreaType.Trim()))
                                   select p).ToList();

                    var tempList = (from p in phnList
                                    where p.AreaType.Trim() == "I"
                                    select p);
                    if (tempList.Count() == 0)
                    {
                        //account.内线计次 = "0";
                        account.内线金额 = "0.00";
                    }
                    else
                    {
                        //account.内线计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.内线金额 = tempList.Sum((item) => item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "C"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.市话计次 = "0";
                        account.市话金额 = "0.00";
                    }
                    else
                    {
                        //account.市话计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.市话金额 = tempList.Sum((item) => item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "F"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.国际计次 = "0";
                        account.国际金额 = "0.00";
                    }
                    else
                    {
                        //account.国际计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.国际金额 = tempList.Sum((item) => item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "L"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.国内计次 = "0";
                        account.国内金额 = "0.00";
                    }
                    else
                    {
                        //account.国内计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.国内金额 = tempList.Sum((item) => item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "M"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.信息计次 = "0";
                        account.信息金额 = "0.00";
                    }
                    else
                    {
                        //account.信息计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.信息金额 = tempList.Sum((item) => item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "O"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.其他计次 = "0";
                        account.其他金额 = "0.00";
                    }
                    else
                    {
                        //account.其他计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.其他金额 = tempList.Sum((item) => item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "P"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.IP计次 = "0";
                        account.IP金额 = "0.00";
                    }
                    else
                    {
                        //account.IP计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.IP金额 = tempList.Sum((item) => item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "S"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.郊县计次 = "0";
                        account.郊县金额 = "0.00";
                    }
                    else
                    {
                        //account.郊县计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.郊县金额 = tempList.Sum((item) => item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "Z"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        //account.专线计次 = "0";
                        account.专线金额 = "0.00";
                    }
                    else
                    {
                        //account.专线计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.专线金额 = tempList.Sum((item) => item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    //account.话单总次数 = phnList.Count();
                    account.总金额 = phnList.Sum((item) => item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value);
                    account.话单总金额 = account.总金额.ToString("0.00");

                    resultList.Add(account);
                }

                TempData["Result"] = resultList;

                if (model.IsExport)
                    return RedirectToAction("Report9Report", "Statistics");
                else
                {
                    TempData["Model"] = model;
                    return RedirectToAction("Report9Result", "Statistics");
                }
            }
            catch (Exception e)
            {
                TempData["Error"] = e.Message;
                return RedirectToAction("Error", "Shared");
            }
        }

        public ActionResult Report9Result()
        {
            Report4ResultModel model = new Report4ResultModel();
            model.List = (List<UIAccount>)TempData["Result"];
            model.总金额 = (model.List.Sum(i => Convert.ToDecimal(i.话单总金额))).ToString("0.00");
            TempData["Result"] = null;
            Report4Model report1Model = (Report4Model)TempData["Model"];
            model.SelectItem = report1Model.SelectItem;
            model.起始时间 = report1Model.起始时间;
            model.起始时间Time = report1Model.起始时间Time;
            model.终止时间 = report1Model.终止时间;
            model.终止时间Time = report1Model.终止时间Time;

            RptImgReport9 report = new RptImgReport9();
            report.Source.DataSource = model.List;
            report.TotalSource.DataSource = model;
            report.ItemCount = model.List.Count;
            using (MemoryStream stream = new MemoryStream())
            {
                report.ExportToHtml(stream);
                StreamReader sr = new StreamReader(stream);
                stream.Position = 0;
                model.Content = sr.ReadToEnd();

            }
            return View(model);
        }

        public FileContentResult Report9Report()
        {
            List<UIAccount> accountList = (List<UIAccount>)TempData["Result"];
            //List<UISourcePhn> resultList = new List<UISourcePhn>();
            ReportResultModel model = new ReportResultModel();
            model.总金额 = (accountList.Sum(i => Convert.ToDecimal(i.话单总金额))).ToString("0.00");

            RptReport9 report = new RptReport9();
            report.Source.DataSource = accountList;
            report.TotalSource.DataSource = model;
            report.ItemCount = accountList.Count;
            using (MemoryStream stream = new MemoryStream())
            {
                report.ExportToXls(stream);
                byte[] b = new byte[stream.Length];
                stream.Position = 0;
                stream.Read(b, 0, b.Length);
                return File(b, "application/octet-stream", "分组话务报表.xls");
            }

        }


        public ActionResult Report5()
        {
            if (Session["User"] == null)
            {
                return this.RedirectToAction("../Account/Login");
            }

            SanDiDbsEntities entities = new SanDiDbsEntities();

            Guid userID = (Guid)Session["UserID"];
            var authroity = (from a in entities.UserAuthority
                             where a.Item == "日话务统计" &&
                                   a.UserID == userID
                             select a).SingleOrDefault();
            if (authroity == null || !authroity.Allow)
            {
                TempData["Error"] = "你没有权限使用这个功能";
                return RedirectToAction("Error", "Shared");
            }

            MenuModel model1 = (MenuModel)Session["Menu"];
            model1.SetMenu("Statistics", "Statistics/Report5");

            Report5Model model = new Report5Model();
            model.开始日期 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            model.结束日期 = model.开始日期.AddMonths(1).AddDays(-1);

            return View(model);
        }

        [HttpPost]
        public ActionResult Report5(Report5Model model)
        {

            try
            {
                SanDiDbsEntities entities = new SanDiDbsEntities();


                List<DateTime> dayList = new List<DateTime>();
                DateTime temp = model.开始日期;
                while (temp <= model.结束日期)
                {
                    dayList.Add(temp);
                    temp = temp.AddDays(1);
                }
                
                
                List<UIAccount> resultList = new List<UIAccount>();

                foreach (DateTime day in dayList)
                {
                    DateTime startDate = day;
                    DateTime endDate = day.AddDays(1).AddMilliseconds(-5);

                    UIAccount account = new UIAccount();
                    account.日期 = day.ToShortDateString();


                    var phnList = (from p in entities.PhnToday
                                   where p.EndTime >= startDate &&
                                         p.EndTime <= endDate &&
                                         p.Slices != null  && p.BaseFee != null && p.ServeFee != null && p.ExtraFee != null
                                   select p).ToList();

                    var tempList = (from p in phnList
                                    where p.AreaType.Trim() == "I"
                                    select p);
                    if (tempList.Count() == 0)
                    {
                        account.内线计次 = "0";
                        account.内线金额 = "0.00";
                    }
                    else
                    {
                        account.内线计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.内线金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "C"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        account.市话计次 = "0";
                        account.市话金额 = "0.00";
                    }
                    else
                    {
                        account.市话计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.市话金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "F"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        account.国际计次 = "0";
                        account.国际金额 = "0.00";
                    }
                    else
                    {
                        account.国际计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.国际金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "L"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        account.国内计次 = "0";
                        account.国内金额 = "0.00";
                    }
                    else
                    {
                        account.国内计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.国内金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "M"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        account.信息计次 = "0";
                        account.信息金额 = "0.00";
                    }
                    else
                    {
                        account.信息计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.信息金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "O"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        account.其他计次 = "0";
                        account.其他金额 = "0.00";
                    }
                    else
                    {
                        account.其他计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.其他金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "P"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        account.IP计次 = "0";
                        account.IP金额 = "0.00";
                    }
                    else
                    {
                        account.IP计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.IP金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "S"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        account.郊县计次 = "0";
                        account.郊县金额 = "0.00";
                    }
                    else
                    {
                        account.郊县计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.郊县金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "Z"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        account.专线计次 = "0";
                        account.专线金额 = "0.00";
                    }
                    else
                    {
                        account.专线计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.专线金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    account.话单总次数 = phnList.Count();
                    account.话单总金额 = phnList.Sum((item) =>item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");

                    resultList.Add(account);
                }

                TempData["Result"] = resultList;

                if (model.IsExport)
                    return RedirectToAction("Report5Report", "Statistics");
                else
                {
                    TempData["Model"] = model;
                    return RedirectToAction("Report5Result", "Statistics");
                }
            }
            catch (Exception e)
            {
                TempData["Error"] = e.Message;
                return RedirectToAction("Error", "Shared");
            }
        }

        public ActionResult Report5Result()
        {
            Report5ResultModel model = new Report5ResultModel();
            model.List = (List<UIAccount>)TempData["Result"];
            model.总金额 = (model.List.Sum(i => Convert.ToDecimal(i.话单总金额))).ToString("0.00");
            TempData["Result"] = null;
            Report5Model report1Model = (Report5Model)TempData["Model"];
            model.开始日期 = report1Model.开始日期;
            model.结束日期 = report1Model.结束日期;
            return View(model);
        }

        public FileContentResult Report5Report()
        {
            List<UIAccount> accountList = (List<UIAccount>)TempData["Result"];
            //List<UISourcePhn> resultList = new List<UISourcePhn>();
            ReportResultModel model = new ReportResultModel();
            model.总金额 = (accountList.Sum(i => Convert.ToDecimal(i.话单总金额))).ToString("0.00");

            RptReport5 report = new RptReport5();
            report.Source.DataSource = accountList;
            report.TotalSource.DataSource = model;
            using (MemoryStream stream = new MemoryStream())
            {
                report.ExportToXls(stream);
                byte[] b = new byte[stream.Length];
                stream.Position = 0;
                stream.Read(b, 0, b.Length);
                return File(b, "application/octet-stream", "日话务报表.xls");
            }

        }

        public ActionResult Report6()
        {
            if (Session["User"] == null)
            {
                return this.RedirectToAction("../Account/Login");
            }

            SanDiDbsEntities entities = new SanDiDbsEntities();

            Guid userID = (Guid)Session["UserID"];
            var authroity = (from a in entities.UserAuthority
                             where a.Item == "收费等级统计" &&
                                   a.UserID == userID
                             select a).SingleOrDefault();
            if (authroity == null || !authroity.Allow)
            {
                TempData["Error"] = "你没有权限使用这个功能";
                return RedirectToAction("Error", "Shared");
            }

            MenuModel model1 = (MenuModel)Session["Menu"];
            model1.SetMenu("Statistics", "Statistics/Report6");

            Report6Model model = new Report6Model();
            model.开始日期 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            model.结束日期 = model.开始日期.AddMonths(1).AddDays(-1);

            return View(model);
        }

        [HttpPost]
        public ActionResult Report6(Report6Model model)
        {

            try
            {
                SanDiDbsEntities entities = new SanDiDbsEntities();

                var gradeList = from obj in entities.OBJDefine
                           where obj.ObjectID.Trim() == "GRADE"
                           orderby obj.ItemID
                           select obj;


                List<UIAccount> resultList = new List<UIAccount>();

                foreach (OBJDefine grade in gradeList)
                {
                    
                    UIAccount account = new UIAccount();
                    account.收费等级 = string.Format("{0}.{1}", grade.ItemID.Trim(), grade.ItemName.Trim());

                    var extensionList = (from e in entities.Extensions
                                         where e.GradeID.Trim() == grade.ItemID.Trim()
                                         select e.ExtNo.Trim());


                    var phnList = (from p in entities.PhnToday
                                   where extensionList.Contains(p.CallerID.Trim()) &&
                                         p.EndTime >= model.开始日期 &&
                                         p.EndTime <= model.结束日期 &&
                                         p.Slices != null  && p.BaseFee != null && p.ServeFee != null && p.ExtraFee != null
                                   select p).ToList();

                    var tempList = (from p in phnList
                                    where p.AreaType.Trim() == "I"
                                    select p);
                    if (tempList.Count() == 0)
                    {
                        account.内线计次 = "0";
                        account.内线金额 = "0.00";
                    }
                    else
                    {
                        account.内线计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.内线金额 = tempList.Sum((item) =>item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "C"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        account.市话计次 = "0";
                        account.市话金额 = "0.00";
                    }
                    else
                    {
                        account.市话计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.市话金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "F"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        account.国际计次 = "0";
                        account.国际金额 = "0.00";
                    }
                    else
                    {
                        account.国际计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.国际金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "L"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        account.国内计次 = "0";
                        account.国内金额 = "0.00";
                    }
                    else
                    {
                        account.国内计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.国内金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "M"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        account.信息计次 = "0";
                        account.信息金额 = "0.00";
                    }
                    else
                    {
                        account.信息计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.信息金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "O"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        account.其他计次 = "0";
                        account.其他金额 = "0.00";
                    }
                    else
                    {
                        account.其他计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.其他金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "P"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        account.IP计次 = "0";
                        account.IP金额 = "0.00";
                    }
                    else
                    {
                        account.IP计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.IP金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "S"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        account.郊县计次 = "0";
                        account.郊县金额 = "0.00";
                    }
                    else
                    {
                        account.郊县计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.郊县金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    tempList = (from p in phnList
                                where p.AreaType.Trim() == "Z"
                                select p);
                    if (tempList.Count() == 0)
                    {
                        account.专线计次 = "0";
                        account.专线金额 = "0.00";
                    }
                    else
                    {
                        account.专线计次 = tempList.Sum((item) => item.Slices.Value).ToString("0.00");
                        account.专线金额 = tempList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                    }

                    account.话单总次数 = phnList.Count();
                    account.话单总金额 = phnList.Sum((item) =>   item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");

                    resultList.Add(account);
                }

                TempData["Result"] = resultList;

                if (model.IsExport)
                    return RedirectToAction("Report6Report", "Statistics");
                else
                {
                    TempData["Model"] = model;
                    return RedirectToAction("Report6Result", "Statistics");
                }
            }
            catch (Exception e)
            {
                TempData["Error"] = e.Message;
                return RedirectToAction("Error", "Shared");
            }
        }

        public ActionResult Report6Result()
        {
            Report6ResultModel model = new Report6ResultModel();
            model.List = (List<UIAccount>)TempData["Result"];
            model.总金额 = (model.List.Sum(i => Convert.ToDecimal(i.话单总金额))).ToString("0.00");
            TempData["Result"] = null;
            Report6Model report1Model = (Report6Model)TempData["Model"];
            model.开始日期 = report1Model.开始日期;
            model.结束日期 = report1Model.结束日期;
            return View(model);
        }

        public FileContentResult Report6Report()
        {
            List<UIAccount> accountList = (List<UIAccount>)TempData["Result"];
            //List<UISourcePhn> resultList = new List<UISourcePhn>();
            ReportResultModel model = new ReportResultModel();
            model.总金额 = (accountList.Sum(i => Convert.ToDecimal(i.话单总金额))).ToString("0.00");

            RptReport6 report = new RptReport6();
            report.Source.DataSource = accountList;
            report.TotalSource.DataSource = model;
            using (MemoryStream stream = new MemoryStream())
            {
                report.ExportToXls(stream);
                byte[] b = new byte[stream.Length];
                stream.Position = 0;
                stream.Read(b, 0, b.Length);
                return File(b, "application/octet-stream", "日话务报表.xls");
            }

        }

        public ActionResult Report7()
        {
            if (Session["User"] == null)
            {
                return this.RedirectToAction("../Account/Login");
            }

            SanDiDbsEntities entities = new SanDiDbsEntities();

            Guid userID = (Guid)Session["UserID"];
            var authroity = (from a in entities.UserAuthority
                             where a.Item == "话务状况统计" &&
                                   a.UserID == userID
                             select a).SingleOrDefault();
            if (authroity == null || !authroity.Allow)
            {
                TempData["Error"] = "你没有权限使用这个功能";
                return RedirectToAction("Error", "Shared");
            }

            MenuModel model1 = (MenuModel)Session["Menu"];
            model1.SetMenu("Statistics", "Statistics/Report7");

            Report7Model model = new Report7Model();
            model.开始日期 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            model.结束日期 = model.开始日期.AddMonths(1).AddDays(-1);

            return View(model);
        }

        [HttpPost]
        public ActionResult Report7(Report7Model model)
        {
            try
            {
                SanDiDbsEntities entities = new SanDiDbsEntities();
                List<Report7Data> resultList = new List<Report7Data>();
                DateTime day = model.开始日期;
                while (day <= model.结束日期)
                {
                    DateTime startTime = day;
                    DateTime endTime = day.AddDays(1).AddMilliseconds(-5);

                    Report7Data data = new Report7Data();
                    data.日期 = day.ToShortDateString();
                    int count = (from p in entities.PhnToday
                                 where p.EndTime > startTime &&
                                       p.EndTime <= endTime
                                 select p).Distinct().Count();
                    data.不明话单数目 = (from p in entities.Unknown
                                 where p.EndTime > startTime &&
                                       p.EndTime <= endTime
                                 select p).Distinct().Count();
                    data.话单总数 = data.不明话单数目 + count;
                    var list = (from p in entities.PhnToday
                                   where p.EndTime > startTime &&
                                         p.EndTime <= endTime
                                   select p);

                    decimal maxFee = 0;
                    foreach (var item in list)
                    {
                        decimal fee = item.ExtraFee.Value + item.BaseFee.Value + item.ServeFee.Value;
                        if (maxFee < fee)
                            maxFee = fee;
                    }
                    data.最大通话金额 = maxFee.ToString("0.00");

                    var temp2 = (from p in entities.PhnToday
                                 where p.EndTime > startTime &&
                                       p.EndTime <= endTime
                                 select p.Duration).Max();
                    data.最大通话时长 = (temp2 == null) ? "0" : temp2.Value.ToString();

                    resultList.Add(data);

                    day = day.AddDays(1);
                }
                
                
                TempData["Result"] = resultList;

                if (model.IsExport)
                    return RedirectToAction("Report7Report", "Statistics");
                else
                {
                    TempData["Model"] = model;
                    return RedirectToAction("Report7Result", "Statistics");
                }
            }
            catch (Exception e)
            {
                TempData["Error"] = e.Message;
                return RedirectToAction("Error", "Shared");
            }
        }

        public ActionResult Report7Result()
        {
            List<Report7Data> accountList = (List<Report7Data>)TempData["Result"];
            TempData["Result"] = null;
            Report7Model report1Model = (Report7Model)TempData["Model"];
            Report7ResultModel model = new Report7ResultModel();
            model.List = accountList;
            model.开始日期 = report1Model.开始日期;
            model.结束日期 = report1Model.结束日期;
            return View(model);
        }

        public FileContentResult Report7Report()
        {
            List<Report7Data> accountList = (List<Report7Data>)TempData["Result"];
            //List<UISourcePhn> resultList = new List<UISourcePhn>();

            RptReport7 report = new RptReport7();
            report.Source.DataSource = accountList;
            using (MemoryStream stream = new MemoryStream())
            {
                report.ExportToXls(stream);
                byte[] b = new byte[stream.Length];
                stream.Position = 0;
                stream.Read(b, 0, b.Length);
                return File(b, "application/octet-stream", "话务状况报表.xls");
            }

        }

        public ActionResult Report8()
        {
            if (Session["User"] == null)
            {
                return this.RedirectToAction("../Account/Login");
            }

            SanDiDbsEntities entities = new SanDiDbsEntities();

            Guid userID = (Guid)Session["UserID"];
            var authroity = (from a in entities.UserAuthority
                             where a.Item == "长途统计" &&
                                   a.UserID == userID
                             select a).SingleOrDefault();
            if (authroity == null || !authroity.Allow)
            {
                TempData["Error"] = "你没有权限使用这个功能";
                return RedirectToAction("Error", "Shared");
            }

            MenuModel model1 = (MenuModel)Session["Menu"];
            model1.SetMenu("Statistics", "Statistics/Report8");

            Report8Model model = new Report8Model();
            model.开始日期 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            model.结束日期 = model.开始日期.AddMonths(1).AddDays(-1);
            
            model.DepartmentList = getDepartmentList(entities, false);

            return View(model);
        }

        [HttpPost]
        public ActionResult Report8(Report8Model model)
        {
            try
            {
                string[] idList = model.SelectItem.Split('!');

                SanDiDbsEntities entities = new SanDiDbsEntities();

                DateTime startDate = new DateTime(model.开始日期.Year, model.开始日期.Month, model.开始日期.Day);
                DateTime endDate = new DateTime(model.结束日期.Year, model.结束日期.Month, model.结束日期.Day, 23,59,59,995);

                var groupList = from e in entities.NGroup
                                where idList.Contains(e.ID)
                                select e;
                List<Report8Data> resultList = new List<Report8Data>();

                foreach (var ngroup in groupList)
                {
                    var temp = ngroup;
                    while (temp.ParentType.Trim() == "Group")
                    {
                        temp = (from g in entities.NGroup
                                where g.ID.Trim() == temp.ParentID
                                select g).SingleOrDefault();
                    }
                    var parent = (from g in entities.NDepartment
                                  where g.ID.Trim() == temp.ParentID
                                  select g).SingleOrDefault();

                    var extensionList = (from e in entities.Extensions
                                         where e.GrpNo.Trim() == ngroup.ID.Trim()
                                         select e.ExtNo.Trim());

                    foreach (var extension in extensionList)
                    {
                        Report8Data account = new Report8Data();
                        account.分组 = ngroup.Name.Trim();
                        account.部门 = parent.Name.Trim();
                        account.分机号码 = extension;

                        var phnList = (from p in entities.PhnToday
                                       where p.CallerID.Trim() == extension &&
                                             p.EndTime >= startDate &&
                                             p.EndTime <= endDate &&
                                             p.Slices != null && p.BaseFee != null && p.ServeFee != null && p.ExtraFee != null
                                       select p).ToList();

                        var tempList = (from p in phnList
                                        where p.AreaType.Trim() == "F"
                                        select p);
                        if (tempList.Count() == 0)
                        {
                            account.国际时长 = "00:00:00";
                            account.国际话费 = "0.00";
                        }
                        else
                        {
                            int hour, minute, secound;
                            secound = tempList.Sum((item) => item.Duration.Value);
                            minute = Math.DivRem(secound, 60, out secound);
                            hour = Math.DivRem(minute, 60, out minute);

                            account.国际时长 = string.Format("{0}:{1}:{2}", hour.ToString("00"), minute.ToString("00"), secound.ToString("00"));
                            account.国际话费 = tempList.Sum((item) => item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                        }

                        tempList = (from p in phnList
                                    where p.AreaType.Trim() == "L"
                                    select p);
                        if (tempList.Count() == 0)
                        {
                            account.国内时长 = "00:00:00";
                            account.国内话费 = "0.00";
                        }
                        else
                        {
                            int hour, minute, secound;
                            secound = tempList.Sum((item) => item.Duration.Value);
                            minute = Math.DivRem(secound, 60, out secound);
                            hour = Math.DivRem(minute, 60, out minute);

                            account.国内时长 = string.Format("{0}:{1}:{2}", hour.ToString("00"), minute.ToString("00"), secound.ToString("00"));
                            account.国内话费 = tempList.Sum((item) => item.BaseFee.Value + item.ServeFee.Value + item.ExtraFee.Value).ToString("0.00");
                        }

                        account.总话费 = (Convert.ToDecimal(account.国内话费) + Convert.ToDecimal(account.国际话费)).ToString("0.00");

                        resultList.Add(account);
                    }
                }

                TempData["Result"] = resultList;

                if (model.IsExport)
                    return RedirectToAction("Report8Report", "Statistics");
                else
                {
                    TempData["Model"] = model;
                    return RedirectToAction("Report8Result", "Statistics");
                }
            }
            catch (Exception e)
            {
                TempData["Error"] = e.Message;
                return RedirectToAction("Error", "Shared");
            }
        }

        public ActionResult Report8Result()
        {
            Report8ResultModel model = new Report8ResultModel();
            model.List = (List<Report8Data>)TempData["Result"];
            model.总金额 = (model.List.Sum(i => Convert.ToDecimal(i.总话费))).ToString("0.00");
            TempData["Result"] = null;
            Report8Model report1Model = (Report8Model)TempData["Model"];
            model.SelectItem = report1Model.SelectItem;
            model.开始日期 = report1Model.开始日期;
            model.结束日期 = report1Model.结束日期;
            return View(model);
        }

        public FileContentResult Report8Report()
        {
            List<Report8Data> accountList = (List<Report8Data>)TempData["Result"];
            //List<UISourcePhn> resultList = new List<UISourcePhn>();
            Report8ResultModel model = new Report8ResultModel();
            model.总金额 = (accountList.Sum(i => Convert.ToDecimal(i.总话费))).ToString("0.00");

            RptReport8 report = new RptReport8();
            report.Source.DataSource = accountList;
            report.TotalSource.DataSource = model;
            using (MemoryStream stream = new MemoryStream())
            {
                report.ExportToXls(stream);
                byte[] b = new byte[stream.Length];
                stream.Position = 0;
                stream.Read(b, 0, b.Length);
                return File(b, "application/octet-stream", "长途报表.xls");
            }
        }

        private List<UIAreaType> loadAreaTypeList()
        {
            SanDiDbsEntities entities = new SanDiDbsEntities();
            var list = from obj in entities.OBJDefine
                       where obj.ObjectID.Trim() == "AREATYPE"
                       orderby obj.ItemID
                       select obj;
            var areaTypeList = new List<UIAreaType>();
            foreach (var item in list)
            {
                UIAreaType areaType = new UIAreaType();
                areaType.ID = item.ItemID.Trim();
                areaType.Name = string.Format("{0}.{1}", item.ItemID.Trim(), item.ItemName.Trim());
                areaTypeList.Add(areaType);
            }

            return areaTypeList;
        }
    }
}
